home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1FHSAZ6 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  10.7 KB  |  344 lines

  1. package com.sun.java.swing.plaf.metal;
  2.  
  3. import com.sun.java.swing.ButtonModel;
  4. import com.sun.java.swing.Icon;
  5. import com.sun.java.swing.JButton;
  6. import com.sun.java.swing.JComponent;
  7. import com.sun.java.swing.JInternalFrame;
  8. import com.sun.java.swing.JMenuBar;
  9. import com.sun.java.swing.UIManager;
  10. import com.sun.java.swing.border.Border;
  11. import com.sun.java.swing.border.EmptyBorder;
  12. import com.sun.java.swing.event.InternalFrameEvent;
  13. import com.sun.java.swing.plaf.ColorUIResource;
  14. import java.awt.Color;
  15. import java.awt.Component;
  16. import java.awt.Container;
  17. import java.awt.Dimension;
  18. import java.awt.Font;
  19. import java.awt.FontMetrics;
  20. import java.awt.Graphics;
  21. import java.awt.LayoutManager;
  22. import java.awt.Toolkit;
  23. import java.awt.event.ActionEvent;
  24. import java.awt.event.ActionListener;
  25. import java.beans.PropertyChangeEvent;
  26. import java.beans.PropertyChangeListener;
  27. import java.beans.PropertyVetoException;
  28. import java.util.EventObject;
  29.  
  30. class MetalInternalFrameTitlePane extends JComponent implements LayoutManager, ActionListener, PropertyChangeListener {
  31.    protected JMenuBar menuBar;
  32.    protected boolean isPalette = false;
  33.    JInternalFrame frame;
  34.    protected JButton iconButton;
  35.    protected JButton maxButton;
  36.    protected JButton closeButton;
  37.    private static final Border handyEmptyBorder = new EmptyBorder(0, 0, 0, 0);
  38.    Icon maxIcon;
  39.    Icon altMaxIcon;
  40.    Icon iconIcon;
  41.    Icon closeIcon;
  42.    int riseWidth = 0;
  43.    int interButtonSpacing = 3;
  44.    MetalBumps activeBumps = new MetalBumps(0, 0, MetalLookAndFeel.getPrimaryControlHighlight(), MetalLookAndFeel.getPrimaryControlDarkShadow(), MetalLookAndFeel.getPrimaryControl());
  45.    MetalBumps inactiveBumps = new MetalBumps(0, 0, MetalLookAndFeel.getControlHighlight(), MetalLookAndFeel.getControlDarkShadow(), MetalLookAndFeel.getControl());
  46.    static Class class$com$sun$java$swing$JInternalFrame;
  47.  
  48.    public MetalInternalFrameTitlePane(JInternalFrame f) {
  49.       this.frame = f;
  50.       ((Component)this).setFont(UIManager.getFont("InternalFrameTitlePane.font"));
  51.       this.maxIcon = (Icon)UIManager.get("InternalFrameTitlePane.maximizeIcon");
  52.       this.altMaxIcon = (Icon)UIManager.get("InternalFrameTitlePane.altMaximizeIcon");
  53.       this.iconIcon = (Icon)UIManager.get("InternalFrameTitlePane.iconizeIcon");
  54.       this.closeIcon = (Icon)UIManager.get("InternalFrameTitlePane.closeIcon");
  55.       this.iconButton = new JButton(this.iconIcon);
  56.       this.iconButton.putClientProperty("paintActive", Boolean.TRUE);
  57.       this.iconButton.setFocusPainted(false);
  58.       this.iconButton.setBorder(handyEmptyBorder);
  59.       this.iconButton.setOpaque(false);
  60.       this.iconButton.addActionListener(this);
  61.       this.iconButton.setActionCommand("Iconify");
  62.       this.maxButton = new JButton(this.maxIcon);
  63.       this.maxButton.putClientProperty("paintActive", Boolean.TRUE);
  64.       this.maxButton.setBorder(handyEmptyBorder);
  65.       this.maxButton.setOpaque(false);
  66.       this.maxButton.setFocusPainted(false);
  67.       this.maxButton.addActionListener(this);
  68.       this.maxButton.setActionCommand("Maximize");
  69.       this.closeButton = new JButton(this.closeIcon);
  70.       this.closeButton.putClientProperty("paintActive", Boolean.TRUE);
  71.       this.closeButton.setBorder(handyEmptyBorder);
  72.       this.closeButton.setOpaque(false);
  73.       this.closeButton.setFocusPainted(false);
  74.       this.closeButton.addActionListener(this);
  75.       this.closeButton.setActionCommand("Close");
  76.       ((Container)this).setLayout(this);
  77.       ((Container)this).add(this.iconButton);
  78.       ((Container)this).add(this.maxButton);
  79.       ((Container)this).add(this.closeButton);
  80.       this.frame.addPropertyChangeListener(this);
  81.    }
  82.  
  83.    public void actionPerformed(ActionEvent e) {
  84.       if ("Close".equals(e.getActionCommand()) && this.frame.isClosable()) {
  85.          this.postClosingEvent(this.frame);
  86.       } else if ("Iconify".equals(e.getActionCommand()) && this.frame.isIconifiable()) {
  87.          if (!this.frame.isIcon()) {
  88.             try {
  89.                this.frame.setIcon(true);
  90.             } catch (PropertyVetoException var9) {
  91.             }
  92.          } else {
  93.             try {
  94.                this.frame.setIcon(false);
  95.             } catch (PropertyVetoException var8) {
  96.             }
  97.          }
  98.  
  99.          ButtonModel model = this.iconButton.getModel();
  100.          if (model != null) {
  101.             model.setRollover(false);
  102.          }
  103.       } else if ("Minimize".equals(e.getActionCommand()) && this.frame.isMaximizable()) {
  104.          try {
  105.             this.frame.setIcon(true);
  106.          } catch (PropertyVetoException var7) {
  107.          }
  108.       } else if ("Maximize".equals(e.getActionCommand()) && this.frame.isMaximizable()) {
  109.          if (!this.frame.isMaximum()) {
  110.             try {
  111.                this.frame.setMaximum(true);
  112.             } catch (PropertyVetoException var6) {
  113.             }
  114.          } else {
  115.             try {
  116.                this.frame.setMaximum(false);
  117.             } catch (PropertyVetoException var5) {
  118.             }
  119.          }
  120.       } else if ("Restore".equals(e.getActionCommand()) && this.frame.isMaximizable() && this.frame.isMaximum()) {
  121.          try {
  122.             this.frame.setMaximum(false);
  123.          } catch (PropertyVetoException var4) {
  124.          }
  125.       } else if ("Restore".equals(e.getActionCommand()) && this.frame.isIconifiable() && this.frame.isIcon()) {
  126.          try {
  127.             this.frame.setIcon(false);
  128.          } catch (PropertyVetoException var3) {
  129.          }
  130.       }
  131.  
  132.    }
  133.  
  134.    public void addLayoutComponent(String name, Component c) {
  135.    }
  136.  
  137.    protected int computeHeight() {
  138.       FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(((Component)this).getFont());
  139.       int fontHeight = fm.getHeight();
  140.       fontHeight += 6;
  141.       int iconHeight = this.frame.getFrameIcon().getIconHeight();
  142.       iconHeight += 7;
  143.       int finalHeight = Math.max(fontHeight, iconHeight);
  144.       return finalHeight;
  145.    }
  146.  
  147.    public Dimension getPreferredSize(Container c) {
  148.       return new Dimension(((Component)c).getSize().width, this.computeHeight());
  149.    }
  150.  
  151.    public void layoutContainer(Container c) {
  152.       int w = ((JComponent)this).getWidth();
  153.       int buttonHeight = this.closeButton.getIcon().getIconHeight();
  154.       int buttonWidth = this.closeButton.getIcon().getIconWidth();
  155.       int x = w;
  156.       if (this.frame.isClosable()) {
  157.          x = w - (buttonWidth + this.interButtonSpacing);
  158.          this.closeButton.setBounds(x, this.interButtonSpacing, buttonWidth, buttonHeight);
  159.       } else if (this.closeButton.getParent() != null) {
  160.          this.closeButton.getParent().remove(this.closeButton);
  161.       }
  162.  
  163.       if (this.frame.isMaximizable()) {
  164.          x -= buttonWidth + this.interButtonSpacing;
  165.          this.maxButton.setBounds(x, this.interButtonSpacing, buttonWidth, buttonHeight);
  166.       } else if (this.maxButton.getParent() != null) {
  167.          this.maxButton.getParent().remove(this.maxButton);
  168.       }
  169.  
  170.       if (this.frame.isIconifiable()) {
  171.          x -= buttonWidth + this.interButtonSpacing;
  172.          this.iconButton.setBounds(x, this.interButtonSpacing, buttonWidth, buttonHeight);
  173.       } else if (this.iconButton.getParent() != null) {
  174.          this.iconButton.getParent().remove(this.iconButton);
  175.       }
  176.  
  177.       this.riseWidth = w - x;
  178.    }
  179.  
  180.    public Dimension minimumLayoutSize(Container c) {
  181.       return this.preferredLayoutSize(c);
  182.    }
  183.  
  184.    public void paint(Graphics g) {
  185.       boolean isSelected = this.frame.isSelected();
  186.       int width = ((JComponent)this).getWidth();
  187.       int height = ((JComponent)this).getHeight();
  188.       Color fillColor = ((JInternalFrame)((Component)this).getParent()).getContentPane().getBackground();
  189.       ColorUIResource background;
  190.       ColorUIResource foreground;
  191.       ColorUIResource shadow;
  192.       ColorUIResource darkShadow;
  193.       ColorUIResource highlight;
  194.       MetalBumps bumps;
  195.       if (isSelected) {
  196.          background = MetalLookAndFeel.getWindowTitleBackground();
  197.          foreground = MetalLookAndFeel.getWindowTitleForeground();
  198.          shadow = MetalLookAndFeel.getPrimaryControlShadow();
  199.          darkShadow = MetalLookAndFeel.getPrimaryControlDarkShadow();
  200.          highlight = MetalLookAndFeel.getPrimaryControlHighlight();
  201.          bumps = this.activeBumps;
  202.       } else {
  203.          background = MetalLookAndFeel.getWindowTitleInactiveBackground();
  204.          foreground = MetalLookAndFeel.getWindowTitleInactiveForeground();
  205.          shadow = MetalLookAndFeel.getControlShadow();
  206.          darkShadow = MetalLookAndFeel.getControlDarkShadow();
  207.          highlight = MetalLookAndFeel.getControlHighlight();
  208.          bumps = this.inactiveBumps;
  209.       }
  210.  
  211.       g.setColor(background);
  212.       g.fillRect(0, 0, width, height);
  213.       g.setColor(highlight);
  214.       g.drawRect(0, 0, width - 1, width - 1);
  215.       g.fillRect(0, 0, 2, 2);
  216.       g.setColor(shadow);
  217.       g.drawLine(1, height - 2, width - 2, height - 2);
  218.       g.drawLine(width - 1, 1, width - 1, height - 2);
  219.       g.setColor(darkShadow);
  220.       g.drawLine(0, height - 1, width, height - 1);
  221.       g.drawLine(0, 0, 0, 0);
  222.       g.drawLine(width - 1, 0, width - 1, 0);
  223.       int titleLength = 0;
  224.       int xOffset = 5;
  225.       if (this.frame.getTitle() != null) {
  226.          Font f = ((Component)this).getFont();
  227.          g.setFont(f);
  228.          FontMetrics fm = g.getFontMetrics();
  229.          fm.getHeight();
  230.          Icon icon = this.frame.getFrameIcon();
  231.          if (icon != null) {
  232.             int iconY = height / 2 - icon.getIconHeight() / 2;
  233.             icon.paintIcon(this.frame, g, xOffset, iconY);
  234.             xOffset += icon.getIconWidth() + 2;
  235.          }
  236.  
  237.          g.setColor(foreground);
  238.          int yOffset = (height - fm.getHeight()) / 2 + fm.getAscent();
  239.          g.drawString(this.frame.getTitle(), xOffset, yOffset);
  240.          titleLength = fm.stringWidth(this.frame.getTitle());
  241.       }
  242.  
  243.       xOffset += titleLength + this.interButtonSpacing;
  244.       int bumpLength = ((JComponent)this).getWidth() - (xOffset + this.riseWidth + this.interButtonSpacing);
  245.       int bumpHeight = ((JComponent)this).getHeight() - 2 * this.interButtonSpacing - 2;
  246.       bumps.setBumpArea(bumpLength, bumpHeight);
  247.       bumps.paintIcon(this, g, xOffset, this.interButtonSpacing);
  248.       if (this.riseWidth != 0) {
  249.          xOffset += bumpLength - 1;
  250.          g.setColor(background);
  251.          g.fillRect(xOffset - 1, height - 7, 7, 3);
  252.          g.setColor(darkShadow);
  253.          g.drawLine(xOffset, height - 2, xOffset + 1, height - 2);
  254.          g.drawLine(xOffset + 2, height - 3, xOffset + 2, height - 3);
  255.          g.drawLine(xOffset + 3, height - 4, xOffset + 4, height - 4);
  256.          g.drawLine(xOffset + 5, height - 5, width, height - 5);
  257.          g.setColor(shadow);
  258.          g.drawLine(xOffset - 1, height - 3, xOffset + 1, height - 3);
  259.          g.drawLine(xOffset + 1, height - 4, xOffset + 2, height - 4);
  260.          g.drawLine(xOffset + 2, height - 5, xOffset + 4, height - 5);
  261.          g.drawLine(xOffset + 4, height - 6, width, height - 6);
  262.          g.setColor(fillColor);
  263.          g.drawLine(xOffset, height - 1, width, height - 1);
  264.          g.drawLine(xOffset + 2, height - 2, width, height - 2);
  265.          g.drawLine(xOffset + 3, height - 3, width, height - 3);
  266.          g.drawLine(xOffset + 5, height - 4, width, height - 4);
  267.       }
  268.  
  269.       ((JComponent)this).paintChildren(g);
  270.    }
  271.  
  272.    void postClosingEvent(JInternalFrame frame) {
  273.       InternalFrameEvent e = new InternalFrameEvent(frame, 25550);
  274.       Class var10000 = class$com$sun$java$swing$JInternalFrame;
  275.       if (var10000 == null) {
  276.          try {
  277.             var10000 = Class.forName("com.sun.java.swing.JInternalFrame");
  278.          } catch (ClassNotFoundException var3) {
  279.             throw new NoClassDefFoundError(((Throwable)var3).getMessage());
  280.          }
  281.  
  282.          class$com$sun$java$swing$JInternalFrame = var10000;
  283.       }
  284.  
  285.       if (var10000.getClassLoader() == null) {
  286.          try {
  287.             Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
  288.             return;
  289.          } catch (SecurityException var4) {
  290.          }
  291.       }
  292.  
  293.       ((Component)frame).dispatchEvent(e);
  294.    }
  295.  
  296.    public Dimension preferredLayoutSize(Container c) {
  297.       return this.getPreferredSize(c);
  298.    }
  299.  
  300.    public void propertyChange(PropertyChangeEvent evt) {
  301.       String prop = evt.getPropertyName();
  302.       JInternalFrame var10000 = (JInternalFrame)((EventObject)evt).getSource();
  303.       boolean value = false;
  304.       if (this.frame.isSelected()) {
  305.          this.iconButton.putClientProperty("paintActive", Boolean.TRUE);
  306.          this.closeButton.putClientProperty("paintActive", Boolean.TRUE);
  307.          this.maxButton.putClientProperty("paintActive", Boolean.TRUE);
  308.          ((Component)this).repaint();
  309.       } else {
  310.          this.iconButton.putClientProperty("paintActive", Boolean.FALSE);
  311.          this.closeButton.putClientProperty("paintActive", Boolean.FALSE);
  312.          this.maxButton.putClientProperty("paintActive", Boolean.FALSE);
  313.          ((Component)this).repaint();
  314.       }
  315.  
  316.       if ("isSelected".equals(prop)) {
  317.          ((Component)this).repaint();
  318.       } else if ("isMaximum".equals(prop)) {
  319.          value = (Boolean)evt.getNewValue();
  320.          if (value) {
  321.             this.maxButton.setIcon(this.altMaxIcon);
  322.          } else {
  323.             this.maxButton.setIcon(this.maxIcon);
  324.          }
  325.       } else if ("isIcon".equals(prop)) {
  326.          value = (Boolean)evt.getNewValue();
  327.          if (value) {
  328.             this.iconButton.setIcon(this.iconIcon);
  329.          } else {
  330.             this.iconButton.setIcon(this.iconIcon);
  331.          }
  332.       }
  333.  
  334.    }
  335.  
  336.    public void removeLayoutComponent(Component c) {
  337.    }
  338.  
  339.    public void setPalette(boolean b) {
  340.       this.isPalette = b;
  341.       ((Component)this).repaint();
  342.    }
  343. }
  344.